//
//  LQL_editorViewController.m
//  Drops
//
//  Created by dllo on 16/5/24.
//  Copyright © 2016年 Drops.er. All rights reserved.
//

#import "LQL_editorViewController.h"
#import <AssetsLibrary/AssetsLibrary.h>
#define CONSTROLPANEL_FONTSIZE 12

@interface LQL_editorViewController ()
@property (nonatomic, retain) UIImageView *imageView;
@property (nonatomic, retain) UIImage *oldImage;//原始图片
@property (nonatomic, retain) UIImage *endImage;//输出图片
@property (nonatomic, retain) UIView *controlView;//控制面板View

@property (nonatomic, retain) UIButton *CISepiaToneButton;
@property (nonatomic, retain) UIButton *CIColorControlsButton;
@property (nonatomic, retain) UIButton *CIExposureAdjustButton;
@property (nonatomic, retain) UIButton *CIColorPosterizeButton;
@property (nonatomic, retain) UIButton *CISharpenLuminanceButton;
@property (nonatomic, retain) UIButton *CIStraightenButton;
@property (nonatomic, retain) UIButton *OKButton;
@property (nonatomic, retain) UIButton *NOButton;
@property (nonatomic, retain) UIImage *OKImage;
@property (nonatomic, assign) NSInteger *i;
//@property (nonatomic, retain) UIButton *CISepiaToneButton;
//@property (nonatomic, retain) UIButton *CISepiaToneButton;
@end

@implementation LQL_editorViewController
{
    CIContext *context;
    CIFilter *sepiaFilter;//棕色
    CIImage *beginImage;
    CIFilter *colorFilter;//亮度饱和对比
    CIFilter *exposureAdjustFilter;//曝光
    CIFilter *posterizeFilter;//层次
    CIFilter *sharpenLuminanceFilter;//锐化
//    CIFilter *straightenFilter;//旋转
}
- (void)viewWillAppear:(BOOL)animated{
    _i = 0;
}
- (void)clickRightButton{
    [self.navigationController popViewControllerAnimated:YES];
        self.editorBlock(self.imageView.image);
}

- (void)backAction:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor blackColor];
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(0, 0, 50, 40);
    [leftBtn setImage:[UIImage imageNamed:@"back.png"] forState:0];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    [leftBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(0, 0, 50, 40);
    [rightBtn setImage:[UIImage imageNamed:@"done.png"] forState:0];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    [rightBtn addTarget:self action:@selector(clickRightButton) forControlEvents:UIControlEventTouchUpInside];
    
    context = [CIContext contextWithOptions:nil];
    self.oldImage = self.inImage;
    [self createView];
    
}
- (void)createView{
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64)];
    self.imageView.backgroundColor = [UIColor clearColor];
    self.imageView.image = self.inImage;
    [self.view addSubview:self.imageView];
    self.imageView.contentMode = UIViewContentModeScaleAspectFit;
    
    //创建6个button
    //第一个
    self.CISepiaToneButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.CISepiaToneButton.frame = CGRectMake(10, [UIScreen mainScreen].bounds.size.height / 3, 40, 40);
    self.CISepiaToneButton.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:0.5];
    self.CISepiaToneButton.tintColor = [UIColor whiteColor];
    self.CISepiaToneButton.layer.cornerRadius = 20;
    self.CISepiaToneButton.layer.masksToBounds = YES;
    [self.CISepiaToneButton setTitle:@"棕色" forState:UIControlStateNormal];
    [self.view addSubview:self.CISepiaToneButton];
    [self.CISepiaToneButton addTarget:self action:@selector(chooseFilter:) forControlEvents:UIControlEventTouchUpInside];
    
    //第二个
    self.CIColorControlsButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.CIColorControlsButton.frame = CGRectMake(10, [UIScreen mainScreen].bounds.size.height / 3 + 60, 40, 40);
    self.CIColorControlsButton.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:0.5];
    self.CIColorControlsButton.tintColor = [UIColor whiteColor];
    self.CIColorControlsButton.layer.cornerRadius = 20;
    self.CIColorControlsButton.layer.masksToBounds = YES;
    [self.CIColorControlsButton setTitle:@"调色" forState:UIControlStateNormal];
    [self.view addSubview:self.CIColorControlsButton];
    [self.CIColorControlsButton addTarget:self action:@selector(chooseFilter:) forControlEvents:UIControlEventTouchUpInside];

    //第三个
    self.CIExposureAdjustButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.CIExposureAdjustButton.frame = CGRectMake(10, [UIScreen mainScreen].bounds.size.height / 3 + 120, 40, 40);
    self.CIExposureAdjustButton.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:0.5];
    self.CIExposureAdjustButton.tintColor = [UIColor whiteColor];
    self.CIExposureAdjustButton.layer.cornerRadius = 20;
    self.CIExposureAdjustButton.layer.masksToBounds = YES;
    [self.CIExposureAdjustButton setTitle:@"曝光" forState:UIControlStateNormal];
    [self.view addSubview:self.CIExposureAdjustButton];
    [self.CIExposureAdjustButton addTarget:self action:@selector(chooseFilter:) forControlEvents:UIControlEventTouchUpInside];

    //第四个,心累啊
    self.CIColorPosterizeButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.CIColorPosterizeButton.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 50, [UIScreen mainScreen].bounds.size.height / 3, 40, 40);
    self.CIColorPosterizeButton.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:0.5];
    self.CIColorPosterizeButton.tintColor = [UIColor whiteColor];
    self.CIColorPosterizeButton.layer.cornerRadius = 20;
    self.CIColorPosterizeButton.layer.masksToBounds = YES;
    [self.CIColorPosterizeButton setTitle:@"层次" forState:UIControlStateNormal];
    [self.view addSubview:self.CIColorPosterizeButton];
    [self.CIColorPosterizeButton addTarget:self action:@selector(chooseFilter:) forControlEvents:UIControlEventTouchUpInside];

    //第五个,太low了
    self.CISharpenLuminanceButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.CISharpenLuminanceButton.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 50, [UIScreen mainScreen].bounds.size.height / 3 + 60, 40, 40);
    self.CISharpenLuminanceButton.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:0.5];
    self.CISharpenLuminanceButton.tintColor = [UIColor whiteColor];
    self.CISharpenLuminanceButton.layer.cornerRadius = 20;
    self.CISharpenLuminanceButton.layer.masksToBounds = YES;
    [self.CISharpenLuminanceButton setTitle:@"锐化" forState:UIControlStateNormal];
    [self.view addSubview:self.CISharpenLuminanceButton];
    [self.CISharpenLuminanceButton addTarget:self action:@selector(chooseFilter:) forControlEvents:UIControlEventTouchUpInside];

//    //第六个!!!
//    self.CIStraightenButton = [UIButton buttonWithType:UIButtonTypeSystem];
//    self.CIStraightenButton.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 50, [UIScreen mainScreen].bounds.size.height / 3 + 120, 40, 40);
//    self.CIStraightenButton.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:0.5];
//    self.CIStraightenButton.tintColor = [UIColor whiteColor];
//    self.CIStraightenButton.layer.cornerRadius = 20;
//    self.CIStraightenButton.layer.masksToBounds = YES;
//    [self.CIStraightenButton setTitle:@"旋转" forState:UIControlStateNormal];
//    [self.view addSubview:self.CIStraightenButton];
//    [self.CIStraightenButton addTarget:self action:@selector(chooseFilter:) forControlEvents:UIControlEventTouchUpInside];
    //第六个!!!
    self.CIStraightenButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.CIStraightenButton.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 50, [UIScreen mainScreen].bounds.size.height / 3 + 120, 40, 40);
    self.CIStraightenButton.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:0.5];
    self.CIStraightenButton.tintColor = [UIColor whiteColor];
    self.CIStraightenButton.layer.cornerRadius = 20;
    self.CIStraightenButton.layer.masksToBounds = YES;
    [self.CIStraightenButton setTitle:@"原图" forState:UIControlStateNormal];
    [self.view addSubview:self.CIStraightenButton];
    [self.CIStraightenButton addTarget:self action:@selector(backToOldImage) forControlEvents:UIControlEventTouchUpInside];

    

    self.NOButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.NOButton.frame = CGRectMake(5, 70, 20, 20);
    self.NOButton.backgroundColor = [UIColor clearColor];
    [self.NOButton setImage:[UIImage imageNamed:@"no.png"] forState:UIControlStateNormal];
    [self.NOButton addTarget:self action:@selector(clickNOButton:) forControlEvents:UIControlEventTouchUpInside];

    self.OKButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.OKButton.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 145, 70, 20, 20);
    self.OKButton.backgroundColor = [UIColor clearColor];
    [self.OKButton setImage:[UIImage imageNamed:@"yes.png"] forState:UIControlStateNormal];
    [self.OKButton addTarget:self action:@selector(clickOKButton:) forControlEvents:UIControlEventTouchUpInside];

    context = [CIContext contextWithOptions:nil];
    //使用GPU渲染，推荐,但注意GPU的CIContext无法跨应用访问，例如直接在UIImagePickerController的完成方法中调用上下文处理就会自动降级为CPU渲染，所以推荐现在完成方法中保存图像，然后在主程序中调用
    
    
}
- (void)backToOldImage{
    self.imageView.image = self.oldImage;
}
- (void)clickOKButton:(UIButton *)button{
    self.OKImage = self.imageView.image;
    [self.controlView removeFromSuperview];
}
- (void)clickNOButton:(UIButton *)button{
    if (0 == self.i) {
        self.imageView.image = self.oldImage;
    } else {
        self.imageView.image = self.OKImage;
    }
    _i ++;
    [self.controlView removeFromSuperview];
}
- (void)chooseFilter:(UIButton *)button{
    [self.controlView removeFromSuperview];
    
    //将图片经过处理转成CIImage形式
    beginImage = [[CIImage alloc] initWithImage:self.imageView.image];
    
    //创建工具栏面板View
    self.controlView = [[UIView alloc] initWithFrame:CGRectMake(60, [UIScreen mainScreen].bounds.size.height / 3, [UIScreen mainScreen].bounds.size.width - 120, 160)];
    
    self.controlView.userInteractionEnabled = YES;
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
    [self.controlView addGestureRecognizer:pan];
    
    
    self.controlView.layer.cornerRadius = 10;
    self.controlView.layer.masksToBounds = YES;
    self.controlView.backgroundColor = button.backgroundColor;
    [self.controlView setAlpha:0];
    [UIView animateWithDuration:1.5 animations:^{
        [self.controlView setAlpha:0.75];
    }];
    
    [self.view addSubview:self.controlView];
    [self.controlView addSubview:self.OKButton];
    [self.controlView addSubview:self.NOButton];
    if ([button.titleLabel.text isEqualToString:@"棕色"]) {
        //      创建滤镜1:棕色
        sepiaFilter = [CIFilter filterWithName:@"CISepiaTone" keysAndValues: @"inputImage", beginImage, @"inputIntensity", @1, nil];
        [self createCISepiaToneSlider];
    }
    else if ([button.titleLabel.text isEqualToString:@"调色"]) {
        //      创建滤镜2:调色
        colorFilter = [CIFilter filterWithName:@"CIColorControls" keysAndValues:@"inputImage", beginImage, nil];
        [self createCIColorControlsSlider];
    }
    else if ([button.titleLabel.text isEqualToString:@"曝光"]) {
        //      创建滤镜3:曝光
        exposureAdjustFilter = [CIFilter filterWithName:@"CIExposureAdjust" keysAndValues:@"inputImage", beginImage, @"inputEV", @0, nil];
        [self createCIExposureAdjustSlider];
    }
    else if ([button.titleLabel.text isEqualToString:@"层次"]) {
        //      创建滤镜4:层次
        posterizeFilter = [CIFilter filterWithName:@"CIColorPosterize" keysAndValues:@"inputImage", beginImage, @"inputLevels", @6, nil];
        [self createCIColorPosterizeSlider];
    }
    else if ([button.titleLabel.text isEqualToString:@"锐化"]) {
        //      创建滤镜5:锐化
        sharpenLuminanceFilter = [CIFilter filterWithName:@"CISharpenLuminance" keysAndValues:@"inputImage", beginImage, @"inputSharpness", @0.4, nil];
        [self createCISharpenLuminanceSlider];
    }
 
//    else if ([button.titleLabel.text isEqualToString:@"旋转"]) {
//        //      创建滤镜6:旋转
//        straightenFilter = [CIFilter filterWithName:@"CIStraightenFilter" keysAndValues:@"inputImage", beginImage, @"inputAngle", @0, nil];
//        [self createCIStraightenSlider];
//    }

}
#pragma mark 拖拽手势点击方法
- (void)panAction:(UIPanGestureRecognizer *)pan
{
    //当前手势经过的坐标
    CGPoint point = [pan translationInView:self.controlView];
    //    self.imageView.transform = CGAffineTransformMakeTranslation(point.x, point.y);
    self.controlView.center = CGPointMake(self.controlView.center.x + point.x, self.controlView.center.y + point.y);
    [pan setTranslation:CGPointZero inView:self.controlView];
}

#pragma mark 棕色
- (void)createCISepiaToneSlider{
    //      棕色调
    UILabel *lbSepiaTone = [[UILabel alloc]initWithFrame:CGRectMake(35, 65, 30, 30)];
    lbSepiaTone.text = @"棕色";
    lbSepiaTone.textColor = [UIColor whiteColor];
    lbSepiaTone.font = [UIFont systemFontOfSize:CONSTROLPANEL_FONTSIZE];
    [self.controlView addSubview:lbSepiaTone];
    UISlider *slSepiaTone = [[UISlider alloc]initWithFrame:CGRectMake(75, 65, [UIScreen mainScreen].bounds.size.width - 235, 30)];
    [self.controlView addSubview: slSepiaTone];
    slSepiaTone.minimumValue = -1;
    slSepiaTone.maximumValue = 1;
    slSepiaTone.value = 0;
    [slSepiaTone setThumbImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateHighlighted];           // 滑块, 要高亮
    [slSepiaTone setThumbImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];
    [slSepiaTone addTarget: self action: @selector(changeSepiaTone:) forControlEvents:UIControlEventValueChanged];
}
#pragma mark 色调
- (void)createCIColorControlsSlider{
    //      亮度
    UILabel *lbBrightness = [[UILabel alloc]initWithFrame:CGRectMake(35, 25, 30, 30)];
    lbBrightness.text = @"亮度";
    lbBrightness.textColor = [UIColor whiteColor];
    lbBrightness.font = [UIFont systemFontOfSize:CONSTROLPANEL_FONTSIZE];
    [self.controlView addSubview: lbBrightness];
    UISlider *sldBrightness = [[UISlider alloc]initWithFrame:CGRectMake(75, 25, [UIScreen mainScreen].bounds.size.width - 235, 30)];
    [self.controlView addSubview: sldBrightness];
    sldBrightness.minimumValue = -1;
    sldBrightness.maximumValue = 1;
    sldBrightness.value = 0;
    [sldBrightness setThumbImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateHighlighted];           // 滑块, 要高亮
    [sldBrightness setThumbImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];
    [sldBrightness addTarget: self action:@selector(changeBrightness:) forControlEvents:UIControlEventValueChanged];
    //      对比度
    UILabel *lbContrast = [[UILabel alloc]initWithFrame:CGRectMake(35, 65, 30, 30)];
    lbContrast.text = @"对比";
    lbContrast.textColor = [UIColor whiteColor];
    lbContrast.font = [UIFont systemFontOfSize: CONSTROLPANEL_FONTSIZE];
    [self.controlView addSubview: lbContrast];
    UISlider *sldContrast = [[UISlider alloc]initWithFrame:CGRectMake(75, 65, [UIScreen mainScreen].bounds.size.width - 235, 30)];
    [self.controlView addSubview:sldContrast];
    sldContrast.minimumValue = 0;
    sldContrast.maximumValue = 2;
    sldContrast.value = 1;
    [sldContrast setThumbImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateHighlighted];           // 滑块, 要高亮
    [sldContrast setThumbImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];
    [sldContrast addTarget:self action:@selector(changeContrast:) forControlEvents:UIControlEventValueChanged];
    //      饱和度(默认为1，大于饱和度增加小于1则降低)
    UILabel *lbSaturation = [[UILabel alloc]initWithFrame:CGRectMake(35, 105, 30, 30)];
    lbSaturation.text = @"饱和";
    lbSaturation.textColor = [UIColor whiteColor];
    lbSaturation.font = [UIFont systemFontOfSize:CONSTROLPANEL_FONTSIZE];
    [self.controlView addSubview:lbSaturation];
    UISlider *sldStaturation = [[UISlider alloc]initWithFrame:CGRectMake(75, 105, [UIScreen mainScreen].bounds.size.width - 235, 30)];
    [self.controlView addSubview: sldStaturation];
    sldStaturation.minimumValue = 0;
    sldStaturation.maximumValue = 2;
    sldStaturation.value = 1;
    [sldStaturation setThumbImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateHighlighted];           // 滑块, 要高亮
    [sldStaturation setThumbImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];
    [sldStaturation addTarget: self action: @selector(changeStaturation:) forControlEvents:UIControlEventValueChanged];
}
#pragma mark 曝光
- (void)createCIExposureAdjustSlider{
    UILabel *lbExposureAdjust = [[UILabel alloc]initWithFrame:CGRectMake(35, 65, 30, 30)];
    lbExposureAdjust.text = @"曝光";
    lbExposureAdjust.textColor = [UIColor whiteColor];
    lbExposureAdjust.font = [UIFont systemFontOfSize:CONSTROLPANEL_FONTSIZE];
    [self.controlView addSubview:lbExposureAdjust];
    UISlider *sldExposureAdjust = [[UISlider alloc]initWithFrame:CGRectMake(75, 65, [UIScreen mainScreen].bounds.size.width - 235, 30)];
    [self.controlView addSubview: sldExposureAdjust];
    sldExposureAdjust.minimumValue = -10;
    sldExposureAdjust.maximumValue = 10;
    sldExposureAdjust.value = 0;
    [sldExposureAdjust setThumbImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateHighlighted];           // 滑块, 要高亮
    [sldExposureAdjust setThumbImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];

    [sldExposureAdjust addTarget: self action: @selector(changeExposureAdjust:) forControlEvents:UIControlEventValueChanged];
}
#pragma mark 层次
- (void)createCIColorPosterizeSlider{
    UILabel *lbPosterize = [[UILabel alloc]initWithFrame:CGRectMake(35, 65, 30, 30)];
    lbPosterize.text = @"层次";
    lbPosterize.textColor = [UIColor whiteColor];
    lbPosterize.font = [UIFont systemFontOfSize:CONSTROLPANEL_FONTSIZE];
    [self.controlView addSubview:lbPosterize];
    UISlider *sldPosterize = [[UISlider alloc]initWithFrame:CGRectMake(75, 65, [UIScreen mainScreen].bounds.size.width - 235, 30)];
    [self.controlView addSubview: sldPosterize];
    sldPosterize.minimumValue = 2;
    sldPosterize.maximumValue = 10;
    sldPosterize.value = 6;
    [sldPosterize setThumbImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateHighlighted];           // 滑块, 要高亮
    [sldPosterize setThumbImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];
    [sldPosterize addTarget: self action: @selector(changePosterize:) forControlEvents:UIControlEventValueChanged];
}
#pragma mark 锐化
- (void)createCISharpenLuminanceSlider{
    UILabel *lbSharpenLuminance = [[UILabel alloc]initWithFrame:CGRectMake(35, 65, 30, 30)];
    lbSharpenLuminance.text = @"锐化";
    lbSharpenLuminance.textColor = [UIColor whiteColor];
    lbSharpenLuminance.font = [UIFont systemFontOfSize:CONSTROLPANEL_FONTSIZE];
    [self.controlView addSubview:lbSharpenLuminance];
    UISlider *sldSharpenLuminance = [[UISlider alloc]initWithFrame:CGRectMake(75, 65, [UIScreen mainScreen].bounds.size.width - 235, 30)];
    [self.controlView addSubview: sldSharpenLuminance];
    sldSharpenLuminance.minimumValue = 0;
    sldSharpenLuminance.maximumValue = 2;
    sldSharpenLuminance.value = 1;
    [sldSharpenLuminance setThumbImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateHighlighted];           // 滑块, 要高亮
    [sldSharpenLuminance setThumbImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];
    [sldSharpenLuminance addTarget: self action: @selector(changeSharpen:) forControlEvents:UIControlEventValueChanged];
}
//#pragma mark 旋转
//- (void)createCIStraightenSlider{
//    UILabel *lbStraightenFilter = [[UILabel alloc]initWithFrame:CGRectMake(35, 65, 30, 30)];
//    lbStraightenFilter.text = @"旋转";
//    lbStraightenFilter.textColor = [UIColor whiteColor];
//    lbStraightenFilter.font = [UIFont systemFontOfSize:CONSTROLPANEL_FONTSIZE];
//    [self.controlView addSubview:lbStraightenFilter];
//    UISlider *sldStraightenFilter = [[UISlider alloc]initWithFrame:CGRectMake(75, 65, [UIScreen mainScreen].bounds.size.width - 235, 30)];
//    [self.controlView addSubview: sldStraightenFilter];
//    sldStraightenFilter.minimumValue = -3.141592653589793;
//    sldStraightenFilter.maximumValue = 3.141592653589793;
//    sldStraightenFilter.value = 0;
//    [sldStraightenFilter setThumbImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateHighlighted];           // 滑块, 要高亮
//    [sldStraightenFilter setThumbImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];
//    [sldStraightenFilter addTarget: self action: @selector(changeStraighten:) forControlEvents:UIControlEventValueChanged];
//}

#pragma mark 调整棕色调
- (void)changeSepiaTone:(UISlider *)slider{
    float slideValue = slider.value;
    [sepiaFilter setValue:@(slideValue)forKey:@"inputIntensity"];
    [self setImage:sepiaFilter];
}
#pragma mark 调整饱和度
-(void)changeStaturation:(UISlider *)slider{
    [colorFilter setValue:[NSNumber numberWithFloat:slider.value] forKey:@"inputSaturation"];//设置滤镜参数
    [self setImage:colorFilter];
}
#pragma mark 调整亮度
-(void)changeBrightness:(UISlider *)slider{
    [colorFilter setValue:[NSNumber numberWithFloat:slider.value] forKey:@"inputBrightness"];
    [self setImage:colorFilter];
}
#pragma mark 调整曝光度
- (void)changeExposureAdjust:(UISlider *)slider{
    [exposureAdjustFilter setValue:[NSNumber numberWithFloat:slider.value] forKey:@"inputEV"];
    [self setImage:exposureAdjustFilter];
}
#pragma mark 调整对比度
- (void)changeContrast:(UISlider *)slider{
    [colorFilter setValue:[NSNumber numberWithFloat:slider.value] forKey:@"inputContrast"];
    [self setImage:colorFilter];
}
#pragma mark 调整层次
- (void)changePosterize:(UISlider *)slider{
    [posterizeFilter setValue:[NSNumber numberWithFloat:slider.value] forKey:@"inputLevels"];
    
    [self setImage:posterizeFilter];
}
#pragma mark 调整锐化度
- (void)changeSharpen:(UISlider *)slider{
    [sharpenLuminanceFilter setValue:[NSNumber numberWithFloat:slider.value] forKey:@"inputSharpness"];
    [self setImage:sharpenLuminanceFilter];
}
//#pragma mark 调整旋转角度
//- (void)changeStraighten:(UISlider *)slider{
//    [straightenFilter setValue:[NSNumber numberWithFloat:slider.value] forKey:@"inputAngle"];
//    [self setImage:straightenFilter];
//    [self setImage:straightenFilter];
//}

//给imageview赋值
- (void)setImage:(CIFilter *)filter{
    CIImage *outputImage = [filter outputImage];
    CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]];
    self.endImage = [UIImage imageWithCGImage:cgimg];
    self.imageView.image = self.endImage;
    CGImageRelease(cgimg);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
